//今儿又是活力满满的一天呢~
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

//获取月份天数
inline int GetMonthDay(int year,int month)
{
	static int DayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = DayArray[month];
	if ((month == 2) && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		day += 1;
	}
	return day;
}
Date::Date(int year, int month, int day)
{
	if (year >0 &&
		month > 0 && month < 13 &&
		day >0 && day < GetMonthDay(year,month))
	{
		_year = year;
		_month = month; 
		_day = day;
	}
	else
	{
		cout << "Wrong Date Des" << endl;
		assert(false);
	}
}
void Date::Print()
{
	cout << _year << "Year" << _month << "Month" << _day << "day" << endl;
}
Date& Date::operator+=(int day)
{
	if (day < 0)//如果day为一个负数
	{
		(*this) -= (-day);
	}
	else
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))//天数不符合当前日期
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	//临时拷贝
	//用这种方式的话，不太好。不如复用刚刚的+=
	Date ret(*this);
	//ret._day += day;
	//while (ret._day > GetMonthDay(ret._year, ret._month))
	//{
	//	ret._day -= day;
	//	ret._month++;
	//}
	ret += day;
	return ret;
}
Date& Date::operator-=(int day)
{
	//如果是负数
	if (day < 0)
	{
		(*this) += (-day);
	}
	else
	{
		_day -= day;
		while (_day <= 0)
		{
			_month--;
			if (_month == 0)//如果是1月
			{
				_year--;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
Date& Date::operator++()//前置++
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)//后置++
{
	Date tmp(*this);
	tmp += 1;
	return tmp;
}
Date& Date::operator--()//前置--
{
	*this -= 1;
	return *this;
}
Date Date::operator--(int)//后置--
{
	Date tmp(*this);
	tmp -= 1;
	return tmp;
}
bool Date::operator>(const Date& d)//比较两个日期的大小
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
		}
	}
	return false;
}

bool Date::operator<(const Date& d)
{
	if ((*this) > d != 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
bool Date::operator>=(const Date& d)
{
	return (*this) > d || (*this) == d;
}
bool Date::operator<=(const Date& d)
{
	return (*this) < d || (*this) == d;
}
bool Date::operator!=(const Date& d)
{
	if (*this == d)
		return 0;
	else
		return 1;
}
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int Gap = 0;//两个日期的差值
	while (min != max)
	{
		++min;
		Gap++;
	}
	return Gap * flag;
}